/**
 * 上海中赢金融信息服务有限公司
 * Copyright (c) 2017-2027 Chinazyjr,Inc.All Rights Reserved.
 */

package com.sys.midware.zookeeper.sample.zkclient;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;

import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.Watcher.Event.KeeperState;
import org.apache.zookeeper.ZooKeeper;

/**
 * <b>ClassName:</b> AbstractZK.java <br/>
 * <b>Description:</b> TODO <br/>
 * <b>Date:</b>     2016年5月4日 上午11:10:13<br/> 
 * @author   mobing
 * @version  	 
 */
public abstract class AbstractZK  implements Watcher{

  //缓存时间  
    private static final int SESSION_TIME   = 2000;     
    protected ZooKeeper zooKeeper;  
    protected CountDownLatch countDownLatch=new CountDownLatch(1);  
 
    public void connect(String hosts) throws IOException, InterruptedException{     
           zooKeeper = new ZooKeeper(hosts,SESSION_TIME,this);     
           countDownLatch.await();     
     }     
 
   /* (non-Javadoc) 
    * @see org.apache.zookeeper.Watcher#process(org.apache.zookeeper.WatchedEvent) 
    */  
   @Override  
   public void process(WatchedEvent event) {  
       if(event.getState()==KeeperState.SyncConnected){  
           countDownLatch.countDown();  
       }  
   }  
     
   public void close() throws InterruptedException{     
       zooKeeper.close();     
   }    
}

